<template>
  <div class="bg-bg_color flex flex-col justify-center" style="padding: 20px">
    <div style="width: 70%" class="flex flex-col">
      <el-descriptions title="个人信息" direction="vertical" :column="4" size="large" border>
        <el-descriptions-item label="姓名">{{ userInfo.userName }}</el-descriptions-item>
        <el-descriptions-item label="电话">{{ userInfo.phoneNub }}</el-descriptions-item>
        <el-descriptions-item label="角色">
          <el-tag v-if="userInfo.role == 'admin'">管理员</el-tag>
          <el-tag v-else type="success">普通用户</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="性别" :span="2"
          ><el-tag v-if="userInfo.sex">男</el-tag> <el-tag v-else type="warning">女</el-tag></el-descriptions-item
        >
        <el-descriptions-item label="地址">{{ userInfo.address }}</el-descriptions-item>
      </el-descriptions>

      <div class="flex justify-end" style="margin-top: 20px">
        <el-button type="warning" size="large" @click="passwordFormVisible = true">修改密码</el-button>
        <el-button type="primary" size="large" @click="editUser">修改信息</el-button>
      </div>
    </div>

    <!-- 编辑 -->
    <el-dialog v-model="editFormVisible" title="编辑用户">
      <el-form ref="ruleFormRef" status-icon label-position="right" label-width="100px" style="max-width: 360px">
        <el-form-item label="姓名">
          <el-input v-model="updateUser.userName" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="updateUser.sex" class="ml-4">
            <el-radio :label="true" size="large">男生</el-radio>
            <el-radio :label="false" size="large">女生</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="updateUser.phoneNub" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="updateUser.address" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="updateUser.role">
            <el-option label="管理员" value="admin" />
            <el-option label="普通用户" value="common" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitEditForm"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 修改密码 -->
    <el-dialog v-model="passwordFormVisible" title="修改密码">
      <el-form ref="ruleFormRef" status-icon label-position="right" label-width="150px" style="max-width: 360px">
        <el-form-item label="旧密码">
          <el-input v-model="updatePasswordData.oldPassword" />
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="updatePasswordData.newPassword" />
        </el-form-item>
        <el-form-item label="再次输入新密码">
          <el-input v-model="updatePasswordData.newPassword2" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="passwordFormVisible = false">取消</el-button>
          <el-button type="primary" @click="updatePasswordApi"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { selectUserById, updateUserById, updatePassword } from "@/api/user";
import { useUserStoreHook } from "@/store/modules/user";
import { message } from "@/utils/message";
import { onMounted, reactive, ref } from "vue";

defineOptions({
  name: "MyInfo"
});

onMounted(() => {
  getUserInfoApi();
});

// 用户数据
const userInfo = reactive({
  id: undefined,
  userName: "",
  phoneNub: "",
  sex: true,
  address: "萍乡学院",
  role: "common"
});

const userId = ref(useUserStoreHook()?.userId);
console.log("当前用户id=", userId);

async function getUserInfoApi() {
  const result = await selectUserById(userId.value);
  console.log("接口数据================", result);
  if (result.code == 200) {
    message("获取成功", { type: "success" });
    // 赋值
    userInfo.id = result.data.id;
    userInfo.userName = result.data.userName;
    userInfo.phoneNub = result.data.phoneNub;
    userInfo.sex = result.data.sex;
    userInfo.address = result.data.address;
    userInfo.role = result.data.role;
  }
}

// ==========编辑用户===========================
const editFormVisible = ref(false);

const updateUser = reactive({
  id: 0,
  userName: "",
  phoneNub: "",
  sex: true,
  address: "萍乡学院",
  role: "common"
});

function editUser() {
  console.log("编辑", userInfo);
  editFormVisible.value = true;

  updateUser.id = userInfo.id;
  updateUser.userName = userInfo.userName;
  updateUser.phoneNub = userInfo.phoneNub;
  updateUser.sex = userInfo.sex;
  updateUser.address = userInfo.address;
  updateUser.role = userInfo.role;
}

async function submitEditForm() {
  console.log("submit!", updateUser);
  const result = await updateUserById(updateUser);
  if (result.code == 200) {
    message("编辑成功", { type: "success" });
    // 关弹窗 刷新数据
    getUserInfoApi();
    editFormVisible.value = false;
  } else {
    message(result.message, { type: "error" });
  }
}

// 修改密码==============================
const passwordFormVisible = ref(false);

const updatePasswordData = reactive({
  oldPassword: "",
  newPassword: "",
  newPassword2: ""
});

async function updatePasswordApi() {
  if (updatePasswordData.newPassword != updatePasswordData.newPassword2) {
    message("两次密码不一样, 请重新输入", { type: "error" });
    return;
  }
  const result = await updatePassword(userInfo.id, updatePasswordData);
  if (result.code == 200) {
    message("密码修改成功, 请重新登入", { type: "success" });
    // 关弹窗 刷新数据
    useUserStoreHook().logOut();
    // passwordFormVisible.value = false;
  } else {
    message(result.message, { type: "error" });
  }
}
</script>

<style scoped></style>
